<template>
  <div class="login">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="登录" name="first">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="名称" prop="name">
            <el-input v-model="ruleForm.name"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="passwd">
            <el-input type="password" v-model="ruleForm.passwd" auto-complete="off"></el-input>
          </el-form-item>
          <el-form-item label="" prop="login_type">
            <el-radio v-model="ruleForm.kind" label="1" border = "true" size="medium">普通用户</el-radio>
            <el-radio v-model="ruleForm.kind" label="2" border = "true" size="medium">管理员</el-radio>
            </el-form-item>
          <el-form-item>
            <el-button type="success" @click="submitForm('ruleForm')">登录</el-button>
            <el-button @click="resetForm('ruleForm')" type="info">重置</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="注册" name="second">
        <register></register>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
import axios from 'axios'

export default {
  data () {
    const validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass')
        }
        callback()
      }
    }
    return {
      activeName: 'first',
      ruleForm: {
        name: '',
        passwd: '',
        kind: '1'
      },
      rules: {
        name: [
          { required: true, message: '请输入您的名称', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        pass: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 选项卡切换
    handleClick (tab, event) {},
    // 重置表单
    resetForm (formName) {
      this.$refs[formName].resetFields()
    },
    // 提交表单
    submitForm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log('start log ...')
          let url = '/login/' + this.ruleForm.name + '/' + this.ruleForm.passwd
          // let url = '/static/api/login.json'
          if (this.ruleForm.kind === '1') {
            axios.get(url)
              .then((res) => {
                console.log(res)
                if (res.data !== -1) {
                  // set token
                  this.$cookies.set('username', this.ruleForm.name)
                  this.$cookies.set('token', '1')
                  this.$store.commit('TID', {tid: res.data})
                  this.$router.push('/home')
                } else {
                  this.$message({
                    type: 'info',
                    message: '用户名或密码错误'
                  })
                }
              })
          } else {
            // '/judgeAdmin/' + this.ruleForm.name + '/' + this.ruleForm.passwd
            axios.get('/judgeAdmin/' + this.ruleForm.name + '/' + this.ruleForm.passwd)
              .then((res) => {
                console.log(res)
                if (res.data !== '-1') {
                  // set token
                  this.$cookies.set('username', this.ruleForm.name)
                  this.$cookies.set('token', '1')
                  this.$router.push('/admin')
                } else {
                  this.$message({
                    type: 'info',
                    message: '用户名或密码错误'
                  })
                }
              })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  components: {
    'register': (resolve) => {
      require(['./register'], resolve)
    }
  }
}

</script>
<style lang="less" rel="stylesheet/less">
.login {
  width: 400px;
  margin: 60px auto;
}

.el-tabs__item {
  text-align: center;
  width: 60px;
}

</style>
